--[[  GET/SET test --]]
local s = ardb.call("set", "k0", "v0")
ardb.assert2(s["ok"] == "OK", s)
local v = ardb.call("get", "k0")
ardb.assert2(v == "v0", v)
ardb.call("set", "k1", "100")
v = ardb.call("get", "k1")
ardb.assert2(v == "100", v)
v = ardb.call("get", "not_exist_key")
ardb.assert2( v == false, v)
s = ardb.call("set", "k0", "vxx", "xx")
ardb.assert2(s["ok"] == "OK", s)
v = ardb.call("get", "k0")
ardb.assert2( v == "vxx", v)
local c = ardb.call("del", "k0")
ardb.assert2( c == 1, c)
s = ardb.call("set", "k0", "v0", "xx")
ardb.assert2( s == false, s)
s = ardb.call("set", "k0", "v0")
ardb.assert2(s["ok"] == "OK", s)
s = ardb.call("set2", "k0", "vxx", "xx")
ardb.assert2(s["ok"] == "OK", s)
v = ardb.call("get", "k0")
ardb.assert2( v == "vxx", v)
ardb.call("del", "k0")
s = ardb.call("set2", "k0", "v0", "xx")
ardb.assert2(s["ok"] == "OK", s)
v = ardb.call("get", "k0")
ardb.assert2( v == false, v)
ardb.call("del", "k1")
s = ardb.call("set", "k1", "v1", "nx")
ardb.assert2(s["ok"] == "OK", s)
s = ardb.call("set", "k1", "v2", "nx")
ardb.assert2( s == false, s)
s = ardb.call("setnx", "k1", "v2")
ardb.assert2( s == 0, s)
ardb.call("del", "k1")
s = ardb.call("setnx", "k1", "v1")
ardb.assert2( s == 1, s)
if ardb.is_merge_supported() then
   s = ardb.call("setnx2", "k1", "v2")
   ardb.assert2(s["ok"] == "OK", s)
   v = ardb.call("get", "k1")
   ardb.assert2( v == "v1", v)
end

--[[  GETSET test --]]
ardb.call("del", "gskey")
s = ardb.call("getset", "gskey", "nv")
ardb.assert2( s == false, s)
ardb.call("set2", "gskey", "nv0")
s = ardb.call("getset", "gskey", "nv1")
ardb.assert2( s == "nv0", s)
v = ardb.call("get", "gskey")
ardb.assert2(v == "nv1", v)

--[[  GetRange/SetRange test --]]
ardb.call("del", "key1")
ardb.call("set2", "key1", "hello,world")
s = ardb.call("setrange", "key1", "6", "redis")
ardb.assert2( s == 11, s)
v = ardb.call("get", "key1")
ardb.assert2( v == "hello,redis", v)
ardb.call("del", "key1")
v = ardb.call("strlen", "key1")
ardb.assert2( v == 0, v)
if ardb.is_merge_supported() then
   s = ardb.call("setrange2", "key1", "6", "redis")
   ardb.assert2(s["ok"] == "OK", s)
   s = ardb.call("setrange2", "key1", "0", "thisis")
   ardb.assert2(s["ok"] == "OK", s)
   v = ardb.call("get", "key1")
   ardb.assert2( v == "thisisredis", v)
   v = ardb.call("strlen", "key1")
   ardb.assert2( v == 11, v)
end

--[[ Merge set test --]]
s = ardb.call("set2", "merge_k0", "1")
ardb.assert2(s["ok"] == "OK", s)
v = ardb.call("get", "merge_k0")
ardb.assert2(v == "1", v)

--[[ Append/Append2 test --]]
ardb.call("del", "mykey")
v = ardb.call("get", "mykey")
ardb.assert2( v == false, v)
s = ardb.call("append", "mykey", "hello")
ardb.assert2(s == 5, s)
s = ardb.call("append", "mykey", " world")
ardb.assert2(s == 11, s)
v = ardb.call("get", "mykey")
ardb.assert2( v == "hello world", v)
ardb.call("del", "mykey")
if ardb.is_merge_supported() then
   s = ardb.call("append2", "mykey", "hello")
   ardb.assert2(s["ok"] == "OK", s)
   v = ardb.call("get", "mykey")
   ardb.assert2( v == "hello", v)
   s = ardb.call("append2", "mykey", " world")
   ardb.assert2(s["ok"] == "OK", s)
   v = ardb.call("get", "mykey")
   ardb.assert2( v == "hello world", v)
end

--[[  MGET/MSET/MSETNX test --]]
s = ardb.call("mset", "k2", "v2", "k3", "v3")
ardb.assert2(s["ok"] == "OK", s)
local vs = ardb.call("mget", "k2", "k3")
ardb.assert2(vs[1] == "v2", vs)
ardb.assert2(vs[2] == "v3", vs)
vs = ardb.call("mget", "not_exist_key", "k3")
ardb.assert2(vs[1] == false, vs)
ardb.assert2(vs[2] == "v3", vs)
c = ardb.call("del", "k2", "k3")
ardb.assert2( c == 2, c)
s = ardb.call("msetnx", "k2", "v2", "k3", "v3")
ardb.assert2(s == 1, s)
s = ardb.call("msetnx", "k2", "v2", "k3", "v3")
ardb.assert2(s == 0, s)

--[[  MSET2/MSETNX2 test --]]
ardb.call("del", "k2", "k3")
s = ardb.call("mset2", "k2", "v20", "k3", "v30")
ardb.assert2(s["ok"] == "OK", s)
local vs = ardb.call("mget", "k2", "k3")
ardb.assert2(vs[1] == "v20", vs)
ardb.assert2(vs[2] == "v30", vs)
ardb.call("del", "k2", "k3")
s = ardb.call("msetnx2", "k2", "v21", "k3", "v31")
ardb.assert2(s["ok"] == "OK", s)
vs = ardb.call("mget", "k2", "k3")
ardb.assert2(vs[1] == "v21", vs)
ardb.assert2(vs[2] == "v31", vs)
s = ardb.call("msetnx2", "k2", "v22", "k3", "v32")
ardb.assert2(s["ok"] == "OK", s)
vs = ardb.call("mget", "k2", "k3")
ardb.assert2(vs[1] == "v21", vs)
ardb.assert2(vs[2] == "v31", vs)

--[[  incr/decr test --]]
ardb.call("del", "lkey")
s = ardb.call("incr", "lkey")
ardb.assert2(s == 1, s)
s = ardb.call("incr", "lkey")
ardb.assert2(s == 2, s)
ardb.call("del", "lkey")
s = ardb.call("decr", "lkey")
ardb.assert2(s == -1, s)
s = ardb.call("decr", "lkey")
ardb.assert2(s == -2, s)
ardb.call("del", "lkey")
if ardb.is_merge_supported() then
   s = ardb.call("incr2", "lkey")
   ardb.assert2(s["ok"] == "OK", s)
   v = ardb.call("get", "lkey")
   ardb.assert2(v == "1", v)
   s = ardb.call("decr2", "lkey")
   ardb.assert2(s["ok"] == "OK", s)
   v = ardb.call("get", "lkey")
   ardb.assert2(v == "0", v)
end
ardb.call("del", "lkey")
s = ardb.call("incrby", "lkey", "101")
ardb.assert2(s == 101, s)
s = ardb.call("decrby", "lkey", "11")
ardb.assert2(s == 90, s)
if ardb.is_merge_supported() then
   s = ardb.call("incrby2", "lkey", "10")
   ardb.assert2(s["ok"] == "OK", s)
   v = ardb.call("get", "lkey")
   ardb.assert2(v == "100", v)
   s = ardb.call("decrby2", "lkey", "1")
   ardb.assert2(s["ok"] == "OK", s)
   v = ardb.call("get", "lkey")
   ardb.assert2(v == "99", v)
end

--[[  incrbyfloat test --]]
ardb.call("del", "fkey")
s = ardb.call("incrbyfloat", "fkey", "1.1")
ardb.assert2(tonumber(s) == 1.1, s)
ardb.call("del", "fkey")
s = ardb.call("incrbyfloat2", "fkey", "1.1")
v = ardb.call("get", "fkey")
ardb.assert2(tonumber(v) == 1.1, v)


